<?php
namespace Home\Model;
use Common\Model\BaseModel;

class CompanyModel extends BaseModel {

    public $tableName = 'company';

    protected $_validate = array(
        array('name','require','企业名称不能为空!'),
        array('user_id','require','请选择企业法人!'),
    );

    /**
     * @param $post
     * @return array
     * 更新或修改公司信息
     */
    public function saveCompany($data){

        $ret = array('status'=>true,'msg'=>'');
        $post = $this->returnData($data);

        if(!$this->create($post)){

            $ret['status'] = false;
            $ret['msg'] = $this->getError();

        }else{

            $res = (null !== $post['company_id']) ? $this->save($post) : $this->add($post);

            if(false !== $res){
                $ret['msg'] = '操作成功';
            }else{
                $ret['status'] = false;
                $ret['msg'] = $this->getDbError();
            }
        }

         return $ret;
    }

    public function returnData($post){

        $return = array();

        $return['company_id']      = !empty($post['company_id']) ? intval($post['company_id']) : null;
        $return['name']            = !empty($post['name']) ? addslashes($post['name']) : '';
        $return['user_id']         = !empty($post['user_id']) ? intval($post['user_id']) : '';
        $return['address']         = !empty($post['address']) ? addslashes($post['address']) : '';
        $return['phone']           = !empty($post['phone']) ? addslashes($post['phone']) : '';
        $return['fax']             = !empty($post['fax']) ? addslashes($post['fax']) : '';
        $return['bank']            = !empty($post['bank']) ? addslashes($post['bank']) : '';
        $return['bank_user']       = !empty($post['bank_user']) ? addslashes($post['bank_user']) : '';
        $return['bank_no']         = !empty($post['bank_no']) ? addslashes($post['bank_no']) : '';

        return $return;
    }
    public function getCompanyList(){

        $list = $this->field('company_id,name')->where('del=1')->select();
        
        return !empty($list) ? $list : array();
    }

    public function getCompanyByUserId($user_id){

        $info = $this->field('company_id,name')->where('user_id={$user_id} and del=1')->limit(1)->select();
        
        return !empty($info) ? $info[0] : array('company_id'=>'','name'=>'');
    }

}